stmn_start = "[%"
stmn_end = "%]"
expr_start = "<%"
expr_end = "%>"

def convert(template, target, globals=[]):
    previousChar = ""
    thisChar = ""
    inPython = False
    pythonString = ""
    with open(template, "r") as input:
        with open(target, 'w') as output:
            while True:
                previousChar = thisChar
                thisChar = input.read(1)
                if thisChar == "":
                    if inPython:
                        raise Exception("the python clauses are not closed")
                    else:
                        output.write(previousChar)
                    return
                elif previousChar + thisChar == stmn_start:
                    inPython = True
                    previousChar = ""
                    thisChar = ""
                elif previousChar + thisChar == stmn_end:
                    inPython = False
                    exec pythonString in globals
                    pythonString = ""
                    thisChar = ""
                elif previousChar + thisChar == expr_start:
                    inPython = True
                    previousChar = ""
                    thisChar = ""
                elif previousChar + thisChar == expr_end:
                    output.write(str(eval(pythonString, globals)))
                    inPython = False
                    pythonString = ""
                    thisChar = ""
                elif inPython:
                    pythonString += previousChar
                else:
                    output.write(previousChar)

import sys
if __name__ == "__main__":
    convert(sys.argv[1], sys.argv[2])
                
   